//
//  MessageSubscription.m
//  cocoapodText
//
//  Created by apple on 16/9/15.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
#import "MessageSubscription.h"
#import <Foundation/Foundation.h>
@interface MessageSubscription ()<FSMyUIDatePickerDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSMutableArray *queryDataInfos;

@end

@implementation MessageSubscription

- (void)viewDidLoad {
    _pageRecord=15;
    _pageAllNum=1;
    [super viewDidLoad];
    [self.view setBackgroundColor:FUIColorFromRGB(0xf4f4f4)];
    [self GetMainContentView];
    _messageMsuId=@"";
    _messageViewType=@"2";
    [_viewTypeMenu setText:@"未查看"];
    [_messageTypeMenu setText:@"全部"];
    _messageDate=[CommonTool getNowDateWithRule:@"yyyyMM"];
    [self.selectTimeMenu setText:_messageDate];
}


/**
 * 边界
 * top:(0, 0, kScreenW, 10)
 * bottom:(0, kScreenH-10, kScreenW,10)
 * left:(0, 0, 10, kScreenH)
 * right:(kScreenW-170, 0, 10, kScreenH)
 */
-(void)GetMainContentView{
    //内容栏
    _messageAllBackView=[[UIView alloc]initWithFrame:CGRectMake([ShiPei CGFloatMakeScale:15], [ShiPei CGFloatMakeScale:15], [ShiPei CGFloatMakeScale:TabChildContentW],  kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [self.view addSubview:_messageAllBackView];
    [_messageAllBackView setBackgroundColor:[UIColor clearColor]];
    [_messageAllBackView setUserInteractionEnabled:YES];
    
    _pickerOverlay=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [ShiPei CGFloatMakeScale:TabChildContentW+10], kScreenH-CommonBarH-kStatusH)];
    [_pickerOverlay setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.17]];
    UITapGestureRecognizer *gestureRecognizer;
    gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                action:@selector(overlaycancel)];
    [_pickerOverlay addGestureRecognizer:gestureRecognizer];
    
    //时间选择picker
    [self initPickerView];
    ///菜单栏
    [self initMenuView];
    ///表格栏
    [self initTableView];
}

-(void)initMenuView{
    UIView *MessageMenuView=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :0 :TabChildContentW :40]];
    [_messageAllBackView addSubview:MessageMenuView];
    [MessageMenuView setUserInteractionEnabled:YES];
    [MessageMenuView setBackgroundColor:[UIColor whiteColor]];
    
    UIImageView *ModuleImage=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:15 :15 :15 :15]];
    [MessageMenuView addSubview:ModuleImage];
    [ModuleImage setImage:[UIImage imageNamed:@"fs_main_id_image.png"]];
    
    UILabel *ModuleLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:40 :10 :120 :20]];
    [MessageMenuView addSubview:ModuleLabel];
    [ModuleLabel setText:@"消息订阅"];
    [ModuleLabel setTextColor:FUIColorFromRGB(0x333333)];
    [ModuleLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:28]]];
    
    UILabel *yuefenLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:210 :0 :50 :40]];
    [MessageMenuView addSubview:yuefenLabel];
    [yuefenLabel setTextAlignment:NSTextAlignmentCenter];
    [yuefenLabel setUserInteractionEnabled:YES];
    [yuefenLabel setText:@"月份"];
    [yuefenLabel setTextColor:FUIColorFromRGB(0x333333)];
    [yuefenLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    //结束时间
    _selectTimeMenu=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:270 :7.5 :80 :25]];
    [MessageMenuView addSubview:_selectTimeMenu];
    [_selectTimeMenu setTextAlignment:NSTextAlignmentCenter];
    [_selectTimeMenu setUserInteractionEnabled:YES];
    [_selectTimeMenu setBackgroundColor:FUIColorFromRGB(0xe1e1e1)];
    [_selectTimeMenu.layer setMasksToBounds:YES];
    [_selectTimeMenu.layer setCornerRadius:10];
    [_selectTimeMenu setUserInteractionEnabled:YES];
    [_selectTimeMenu setText:@""];
    [_selectTimeMenu setTextColor:FUIColorFromRGB(0x1b75cc)];
    [_selectTimeMenu setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectTimeClick:)];
    [_selectTimeMenu addGestureRecognizer:singleTap1];
    
    UILabel *messageTypeLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:360 :0 :100 :40]];
    [MessageMenuView addSubview:messageTypeLabel];
    [messageTypeLabel setTextAlignment:NSTextAlignmentCenter];
    [messageTypeLabel setUserInteractionEnabled:YES];
    [messageTypeLabel setText:@"订阅类型"];
    [messageTypeLabel setTextColor:FUIColorFromRGB(0x333333)];
    [messageTypeLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    //订阅类型
    _messageTypeMenu=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:460 :7.5 :80 :25]];
    [MessageMenuView addSubview:_messageTypeMenu];
    [_messageTypeMenu setTextAlignment:NSTextAlignmentCenter];
    [_messageTypeMenu setUserInteractionEnabled:YES];
    [_messageTypeMenu setBackgroundColor:FUIColorFromRGB(0xe1e1e1)];
    [_messageTypeMenu.layer setMasksToBounds:YES];
    [_messageTypeMenu.layer setCornerRadius:10];
    [_messageTypeMenu setUserInteractionEnabled:YES];
    [_messageTypeMenu setText:@""];
    [_messageTypeMenu setTextColor:FUIColorFromRGB(0x1b75cc)];
    [_messageTypeMenu setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    UITapGestureRecognizer *singleTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(messageType:)];
    [_messageTypeMenu addGestureRecognizer:singleTap2];
    
    UILabel *viewTypeLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:550 :0 :100 :40]];
    [MessageMenuView addSubview:viewTypeLabel];
    [viewTypeLabel setTextAlignment:NSTextAlignmentCenter];
    [viewTypeLabel setUserInteractionEnabled:YES];
    [viewTypeLabel setText:@"查看类型"];
    [viewTypeLabel setTextColor:FUIColorFromRGB(0x333333)];
    [viewTypeLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    //查看类型
    _viewTypeMenu=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:650 :7.5 :80 :25]];
    [MessageMenuView addSubview:_viewTypeMenu];
    [_viewTypeMenu setTextAlignment:NSTextAlignmentCenter];
    [_viewTypeMenu setUserInteractionEnabled:YES];
    [_viewTypeMenu setBackgroundColor:FUIColorFromRGB(0xe1e1e1)];
    [_viewTypeMenu.layer setMasksToBounds:YES];
    [_viewTypeMenu.layer setCornerRadius:10];
    [_viewTypeMenu setUserInteractionEnabled:YES];
    [_viewTypeMenu setText:@""];
    [_viewTypeMenu setTextColor:FUIColorFromRGB(0x1b75cc)];
    [_viewTypeMenu setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    UITapGestureRecognizer *singleTap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewType:)];
    [_viewTypeMenu addGestureRecognizer:singleTap3];
    
    UIButton *queryButton=[[UIButton alloc]initWithFrame:[ShiPei CGRectMakeScale:760 :15/2 :50 :25]];
    [MessageMenuView addSubview:queryButton];
    [queryButton setBackgroundImage:[CommonTool createImageWithColor:FUIColorFromRGB(0x3388cb) Frame:[ShiPei CGRectMakeScale:0 :0 :50 :25]] forState:UIControlStateNormal];
    [queryButton setBackgroundImage:[CommonTool createImageWithColor:FUIColorFromRGB(0x639ed7)  Frame:[ShiPei CGRectMakeScale:0 :0 :50 :25]] forState:UIControlStateHighlighted];
    [queryButton.layer setMasksToBounds:YES];
    [queryButton.layer setCornerRadius:5];
    [queryButton setTitle:@"查询" forState:UIControlStateNormal];
    [queryButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [queryButton.titleLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:26]]];
    [queryButton addTarget:self action:@selector(onClickQuery) forControlEvents:UIControlEventTouchUpInside];
 
}

-(void)initTableView{
    
    UIView *messageAllView=[[UIView alloc]initWithFrame:CGRectMake(0, [ShiPei CGFloatMakeScale:55], [ShiPei CGFloatMakeScale:TabChildContentW], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:80]-[ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [_messageAllBackView addSubview:messageAllView];
    [messageAllView setUserInteractionEnabled:YES];
    [messageAllView setBackgroundColor:[UIColor whiteColor]];
    
    MessageTopView *messageTopView=[[MessageTopView alloc]initWithFrame:[ShiPei CGRectMakeScale:15 :20 :800 :35]];
    [messageAllView addSubview:messageTopView];
    [messageTopView setBackgroundColor:FUIColorFromRGB(0x639ed7)];
    [messageTopView setUserInteractionEnabled:YES];
    [messageTopView setName:@"订阅类型"];
    [messageTopView setType:@"用能类型"];
    [messageTopView setTime:@"监测时间"];
    [messageTopView setContent:@"监测内容"];
    [messageTopView setNumber:@"监测值"];
    [messageTopView setNumber2:@"实际值"];
    [messageTopView setStatus:@"状态"];
    
    _messageInfoTable=[[UITableView alloc]initWithFrame:CGRectMake([ShiPei CGFloatMakeScale:15], [ShiPei CGFloatMakeScale:55], [ShiPei CGFloatMakeScale:800], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:150]-[ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [messageAllView addSubview:_messageInfoTable];
    [_messageInfoTable setBackgroundColor:[UIColor clearColor]];
    [_messageInfoTable setUserInteractionEnabled:YES];
    [_messageInfoTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_messageInfoTable setDelegate:self];
    [_messageInfoTable setDataSource:self];
    
    // Set the callback（Once you enter the refresh status，then call the action of target，that is call [self loadMoreData]）
    _messageInfoTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

-(void)loadMoreData{
    if (_pageNum+1>_pageAllNum) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 拿到当前的上拉刷新控件，结束刷新状态
            [_messageInfoTable.mj_footer endRefreshing];
        });
    }else{
        APIMessageManager *manager=[APIMessageManager alloc];
        manager.delegate=self;
        [manager fsgy_api_info_message_subscribe_withFalg:MSLoadMore MsuId:_messageMsuId Issub:_messageViewType Year:_messageDate PageNum:[NSString stringWithFormat:@"%d",_pageNum+1] RecoredCount:[NSString stringWithFormat:@"%d",_recordCount] PageRecord:@"15"];
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            [_messageInfoTable reloadData];
            // 拿到当前的上拉刷新控件，结束刷新状态
            [_messageInfoTable.mj_footer endRefreshing];
        });
    }
    
}


-(void)initPickerView{
    _is_picker_show=NO;
    _datePicker=[[MyUIDatePicker alloc]initWithFrame:CGRectMake(0, kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:250], [ShiPei CGFloatMakeScale:860], [ShiPei CGFloatMakeScale:250])];
    [_datePicker setDelegate:self];
    [_pickerOverlay addSubview:_datePicker];
}

-(void)onMessageManager:(APIMessageManager *)manager result:(NSDictionary *)result Flag:(NSString *)flag{
    if ([flag isEqualToString:MSLoadMore]) {
        //解析数据
        NSArray *list=[result objectForKey:@"list"];
        if ([list count]>0) {
            _pageNum++;
            NSMutableArray *models=[NSMutableArray array];
            for (int j=0; j<[_queryDataInfos count]; j++) {
                [models addObject:[_queryDataInfos objectAtIndex:j]];
            }
            _queryDataInfos =[NSMutableArray array];
            //颜色问题
            if ([_queryDataInfos count]%2==0) {
                for (int i=0; i<[list count]; i++) {
                    NSDictionary *messageDic=[list objectAtIndex:i];
                    NSString *msu_name=[CommonTool getStringFromNSDictionary:messageDic key:@"msu_name" defaultValue:@""];
                    NSString *es_name=[CommonTool getStringFromNSDictionary:messageDic key:@"es_name" defaultValue:@""];
                    NSDictionary *timeDic=[messageDic objectForKey:@"opertime"];
                    NSString *time=[CommonTool getNowDateTimeByTimestamp:[CommonTool getStringFromNSDictionary:timeDic key:@"time" defaultValue:@""] Rule:@"yyyy-MM-dd HH:MM:ss"];
                    NSString *jccontent=[CommonTool getStringFromNSDictionary:messageDic key:@"jccontent" defaultValue:@""];
                    NSString *jdval=[CommonTool getStringFromNSDictionary:messageDic key:@"jdval" defaultValue:@""];
                    NSString *sjval=[CommonTool getStringFromNSDictionary:messageDic key:@"sjval" defaultValue:@""];
                    NSString *issuedName=[CommonTool getStringFromNSDictionary:messageDic key:@"issuedName" defaultValue:@"已查看"];
                    messageInfo *model;
                    if (i%2==0) {
                        model=[messageInfo messageInfoWithName:msu_name type:es_name time:time content:jccontent number:jdval number2:sjval color:@"1" status:issuedName];
                        [models addObject:model];
                    }else{
                        model=[messageInfo messageInfoWithName:msu_name type:es_name time:time content:jccontent number:jdval number2:sjval color:@"0" status:issuedName];
                        [models addObject:model];
                    }
                }
            }else{
                for (int i=0; i<[list count]; i++) {
                    NSDictionary *messageDic=[list objectAtIndex:i];
                    NSString *msu_name=[CommonTool getStringFromNSDictionary:messageDic key:@"msu_name" defaultValue:@""];
                    NSString *es_name=[CommonTool getStringFromNSDictionary:messageDic key:@"es_name" defaultValue:@""];
                    NSDictionary *timeDic=[messageDic objectForKey:@"opertime"];
                    NSString *time=[CommonTool getNowDateTimeByTimestamp:[CommonTool getStringFromNSDictionary:timeDic key:@"time" defaultValue:@""] Rule:@"yyyy-MM-dd HH:MM:ss"];
                    NSString *jccontent=[CommonTool getStringFromNSDictionary:messageDic key:@"jccontent" defaultValue:@""];
                    NSString *jdval=[CommonTool getStringFromNSDictionary:messageDic key:@"jdval" defaultValue:@""];
                    NSString *sjval=[CommonTool getStringFromNSDictionary:messageDic key:@"sjval" defaultValue:@""];
                    NSString *issuedName=[CommonTool getStringFromNSDictionary:messageDic key:@"issuedName" defaultValue:@"已查看"];
                    messageInfo *model;
                    if (i%2==0) {
                        model=[messageInfo messageInfoWithName:msu_name type:es_name time:time content:jccontent number:jdval number2:sjval color:@"0" status:issuedName];
                        [models addObject:model];
                    }else{
                        model=[messageInfo messageInfoWithName:msu_name type:es_name time:time content:jccontent number:jdval number2:sjval color:@"1" status:issuedName];
                        [models addObject:model];
                    }
                }
            }
            _queryDataInfos=[models copy];
        }
        

    }else if ([flag isEqualToString:MSInit]) {
        _pageNum=1;
        _recordCount=[[CommonTool getStringFromNSDictionary:result key:@"list.page.recordCount" defaultValue:@"0"] intValue];
        _pageAllNum=_recordCount/15;
        int yushu=_recordCount%15;
        if (yushu!=0) {
            _pageAllNum=_pageAllNum+1;
        }
        //解析数据
        NSArray *list=[result objectForKey:@"list"];
        NSMutableArray *models=[NSMutableArray array];
        for (int i=0; i<[list count]; i++) {
            NSDictionary *messageDic=[list objectAtIndex:i];
            NSString *msu_name=[CommonTool getStringFromNSDictionary:messageDic key:@"msu_name" defaultValue:@""];
            NSString *es_name=[CommonTool getStringFromNSDictionary:messageDic key:@"es_name" defaultValue:@""];
            NSDictionary *timeDic=[messageDic objectForKey:@"opertime"];
            NSString *time=[CommonTool getNowDateTimeByTimestamp:[CommonTool getStringFromNSDictionary:timeDic key:@"time" defaultValue:@""] Rule:@"yyyy-MM-dd HH:MM:ss"];
            NSString *jccontent=[CommonTool getStringFromNSDictionary:messageDic key:@"jccontent" defaultValue:@""];
            NSString *jdval=[CommonTool getStringFromNSDictionary:messageDic key:@"jdval" defaultValue:@""];
            NSString *sjval=[CommonTool getStringFromNSDictionary:messageDic key:@"sjval" defaultValue:@""];
            NSString *issuedName=[CommonTool getStringFromNSDictionary:messageDic key:@"issuedName" defaultValue:@"已查看"];
            messageInfo *model;
            if (i%2==0) {
                model=[messageInfo messageInfoWithName:msu_name type:es_name time:time content:jccontent number:jdval number2:sjval color:@"1" status:issuedName];
                [models addObject:model];
            }else{
                model=[messageInfo messageInfoWithName:msu_name type:es_name time:time content:jccontent number:jdval number2:sjval color:@"0" status:issuedName];
                [models addObject:model];
            }
        }
        _queryDataInfos=[models copy];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            // 处理耗时操作的代码块...
            //通知主线程刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                //回调或者说是通知主线程刷新
                [_messageInfoTable reloadData];
            });
            
        });
    }
}


-(void)onQueren:(MyUIDatePicker *)datePicker{
    //201607（时间格式）
    NSDateFormatter *dateFormat = [ [NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyyMM"];
    NSString *newDateStr = [dateFormat stringFromDate:datePicker.datePicker.date];
    [self.selectTimeMenu setText:newDateStr];
    _messageDate=newDateStr;
    [_pickerOverlay removeFromSuperview];
    _is_picker_show=NO;
}

-(void)onCancel:(MyUIDatePicker *)datePicker{
    [_pickerOverlay removeFromSuperview];
    _is_picker_show=NO;
}

-(void)overlaycancel{
    [self onCancel:_datePicker];
}

-(void)selectTimeClick:(id)sender{
    if (!_is_picker_show) {
        [self.view addSubview:_pickerOverlay];
        _is_picker_show=YES;
    }
}

-(void)onClickQuery{
    APIMessageManager *manager=[APIMessageManager alloc];
    manager.delegate=self;
    [manager fsgy_api_info_message_subscribe_withFalg:MSInit MsuId:_messageMsuId Issub:_messageViewType Year:_messageDate PageNum:@"" RecoredCount:@"" PageRecord:@""];
}

#pragma mark - 数据源方法
#pragma mark 返回分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//设置rowHeight
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [ShiPei CGFloatMakeScale:35];
}

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.queryDataInfos.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    //显示详细监测内容
    messageInfo *temMessage=[self.queryDataInfos objectAtIndex:indexPath.row];
    UIAlertView *content=[[UIAlertView alloc]initWithTitle:@"监测内容" message:temMessage.content delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];
    [content show];
}


#pragma mark返回每行的单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //1.到缓存中去取cell
    static NSString *ID=@"ID";
    //2.没有则创建cell
    MessageTableCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[[MessageTableCell
               alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    //3.设置cell的数据
    cell.messageinfo=self.queryDataInfos[indexPath.row];
    //4.返回cell
    return cell;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action:) name:@"MessageSubscriptionAction" object:nil];
    /**
     *标识跳转到此页面的动作
     * menuClick 主菜单点击选择
     * messageNewClick 顶部菜单最新消息点击
     * warnInfoClick 顶部菜单预警点击
     */
    NSString *action=[CommonTool getValueFromKey:@"MessageSubscriptionAction" defaultValue:menuClick];
    if([action isEqualToString:warnInfo]){
        /**
         *标识预警信息种类
         * warnDing 定额预警
         * warnZhi 指标预警
         */
        NSString *witchWarn=[CommonTool getValueFromKey:@"MessageWarnType" defaultValue:@""];
        if ([witchWarn isEqualToString:warnDing]) {
            _messageMsuId=@"1";
            [_messageTypeMenu setText:@"定额预警"];
        }else if ([witchWarn isEqualToString:warnZhi]){
            _messageMsuId=@"2";
            [_messageTypeMenu setText:@"指标预警"];
        }
    }else{
        _messageMsuId=@"";
        [_messageTypeMenu setText:@"全部"];
    }
    [self onClickQuery];
}

-(void)action:(NSNotification *)notification{
    /**
     *标识预警信息种类
     * warnDing 定额预警
     * warnZhi 指标预警
     */
    NSString *witchWarn=[CommonTool getValueFromKey:@"MessageWarnType" defaultValue:@""];
    if ([witchWarn isEqualToString:warnDing]) {
        _messageMsuId=@"1";
        [_messageTypeMenu setText:@"定额预警"];
    }else if ([witchWarn isEqualToString:warnZhi]){
        _messageMsuId=@"2";
        [_messageTypeMenu setText:@"指标预警"];
    }
    [self onClickQuery];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //消息
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MessageSubscriptionAction" object:nil];
}

-(void)viewType:(UITapGestureRecognizer *)recognizer{
    /**
     * currentMenuType
     * messageMsuId 订阅类型
     * messageViewType 查看类型
     */
    _currentMenuType=@"messageViewType";
    NSMutableArray *models=[NSMutableArray array];
    KxMenuItem *model;
    model=[KxMenuItem menuItem:@"未查看" image:nil target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    model=[KxMenuItem menuItem:@"已查看" image:nil target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    [KxMenu setTitleFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    Color color,color2;
    color.R=0;
    color.G=0;
    color.B=0;
    color2.R=1;
    color2.G=1;
    color2.B=1;
    OptionalConfiguration options;
    options.arrowSize=9;//指示箭头大小
    options.marginXSpacing= 7;  //MenuItem左右边距
    options.marginYSpacing= 9;  //MenuItem上下边距
    options.intervalSpacing= 25;  //MenuItemImage与MenuItemTitle的间距
    options.menuCornerRadius= 6.5;  //菜单圆角半径
    options.maskToBackground= true;  //是否添加覆盖在原View上的半透明遮罩
    options.shadowOfMenu= false;  //是否添加菜单阴影
    options.hasSeperatorLine= true;  //是否设置分割线
    options.seperatorLineHasInsets= false;  //是否在分割线两侧留下Insets
    options.textColor=color;  //menuItem字体颜色
    options.menuBackgroundColor= color2;  //菜单的底色
    [KxMenu showMenuInView:_messageAllBackView fromRect:recognizer.view.frame menuItems:models withOptions:options];
}

-(void)messageType:(UITapGestureRecognizer *)recognizer{
    
    /**
     * currentMenuType
     * messageMsuId 订阅类型
     * messageViewType 查看类型
     */
    _currentMenuType=@"messageMsuId";
    NSMutableArray *models=[NSMutableArray array];
    KxMenuItem *model;
    model=[KxMenuItem menuItem:@"全部" image:nil target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    model=[KxMenuItem menuItem:@"定额预警" image:nil target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    model=[KxMenuItem menuItem:@"指标预警" image:nil target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    model=[KxMenuItem menuItem:@"定额下达" image:nil target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    model=[KxMenuItem menuItem:@"指标下达" image:nil target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    model=[KxMenuItem menuItem:@"能源调度下达" image:nil target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    model=[KxMenuItem menuItem:@"可调企业基础数据下达" image:nil target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    
    [KxMenu setTitleFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    Color color,color2;
    color.R=0;
    color.G=0;
    color.B=0;
    color2.R=1;
    color2.G=1;
    color2.B=1;
    OptionalConfiguration options;
    options.arrowSize=9;//指示箭头大小
    options.marginXSpacing= 7;  //MenuItem左右边距
    options.marginYSpacing= 9;  //MenuItem上下边距
    options.intervalSpacing= 25;  //MenuItemImage与MenuItemTitle的间距
    options.menuCornerRadius= 6.5;  //菜单圆角半径
    options.maskToBackground= true;  //是否添加覆盖在原View上的半透明遮罩
    options.shadowOfMenu= false;  //是否添加菜单阴影
    options.hasSeperatorLine= true;  //是否设置分割线
    options.seperatorLineHasInsets= false;  //是否在分割线两侧留下Insets
    options.textColor=color;  //menuItem字体颜色
    options.menuBackgroundColor= color2;  //菜单的底色
    [KxMenu showMenuInView:_messageAllBackView fromRect:recognizer.view.frame menuItems:models withOptions:options];
}

-(void)KxMenuItemClick:(id)sender{
    KxMenuItem *model=sender;
    NSString *type= model.title;
    /**
     * currentMenuType
     * messageMsuId 订阅类型
     * messageViewType 查看类型
     */
    if ([_currentMenuType isEqualToString:@"messageMsuId"]) {
        //msuId=1 备注：3=定额下达, 2=指标预警, 1=定额预警, 6=可调企业基础数据下达, 5=能源调度下达, 4=指标下达
        if ([type isEqualToString:@"全部"]) {
            _messageMsuId=@"";
        }else if ([type isEqualToString:@"定额预警"]) {
            _messageMsuId=@"1";
        }else if ([type isEqualToString:@"指标预警"]) {
            _messageMsuId=@"2";
        }else if ([type isEqualToString:@"定额下达"]) {
            _messageMsuId=@"3";
        }else if ([type isEqualToString:@"指标下达"]) {
            _messageMsuId=@"4";
        }else if ([type isEqualToString:@"能源调度下达"]) {
            _messageMsuId=@"5";
        }else if ([type isEqualToString:@"可调企业基础数据下达"]) {
            _messageMsuId=@"6";
        }else{
            _messageMsuId=@"1";
        }
        [self.messageTypeMenu setText:type];
    }else if([_currentMenuType isEqualToString:@"messageViewType"]){
        if ([type isEqualToString:@"已查看"]) {
            _messageViewType=@"1";
        }else{
            _messageViewType=@"2";
        }
        [self.viewTypeMenu setText:type];
    }
}
@end
